package it.maganar.lesscaos.client;



import it.maganar.lesscaos.session.ServiceGateRemote;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;


public class LessCaosMainWidget {

	private JFrame frame;
	private JPanel pannelloTitolo;
	private JPanel pannelloCorpo;
	private JLabel titolo;
	
	ServiceGateRemote serviceGate;
	RegistrationWidget registrationWidget;
	LoginWidget loginWidget;
	
	private JPanel pannelloMenu;
	private JButton bottoneDisponibilita;
	private JButton bottonePartecipazioni;
	private JButton bottoneNegoziazioni;
	private JPanel pannelloSinistro;
	private JPanel pannelloDestro;
	private JButton bottoneLogout;
	private JPanel pannelloLogging;
	private JButton bottoneRegistrazione;
	private JButton bottoneLogin;
	
	
	
	LessCaosMainWidget(){
		
		serviceGate = NetworkConnector.sgr;
		final LessCaosMainWidget lcmw = this;
		
		// Inizializzazione Frame
		frame = new JFrame("LESS-CAOS");
		frame.setLayout(new BorderLayout());
		
		
		// Inizializzazione e Inserimento pannelli principali
		pannelloTitolo = new JPanel();
		pannelloTitolo.setLayout(new FlowLayout());
		pannelloTitolo.setBackground(Color.DARK_GRAY);
		frame.add(pannelloTitolo, BorderLayout.NORTH);
		
		pannelloCorpo = new JPanel();
		pannelloCorpo.setLayout(new FlowLayout());
		frame.add(pannelloCorpo, BorderLayout.CENTER);
		
		pannelloSinistro = new JPanel();
		pannelloSinistro.setLayout(new FlowLayout());
		pannelloSinistro.setBackground(Color.GRAY);
		frame.add(pannelloSinistro, BorderLayout.WEST);
		
		pannelloMenu = new JPanel();		
		pannelloMenu.setLayout(new GridLayout(3,1));
		pannelloSinistro.add(pannelloMenu); 
		
		pannelloDestro = new JPanel();
		pannelloDestro.setLayout(new FlowLayout());
		pannelloDestro.setBackground(Color.GRAY);
		frame.add(pannelloDestro, BorderLayout.EAST);
		
		pannelloLogging = new JPanel();
		pannelloLogging.setLayout(new GridLayout(3,1));
		pannelloDestro.add(pannelloLogging);
		
	
		//Inizializzazione e Inserimento Bottoni Menu
		bottoneDisponibilita = new JButton("Disponibilita");
		bottonePartecipazioni = new JButton("Partecipazioni");
		bottoneNegoziazioni = new JButton("Negoziazioni");
		
		pannelloMenu.add(bottoneDisponibilita);
		pannelloMenu.add(bottonePartecipazioni);
		pannelloMenu.add(bottoneNegoziazioni);
		
		bottoneLogout = new JButton("Logout");
		bottoneRegistrazione = new JButton("Registrazione");
		bottoneLogin = new JButton("Login");
		
		pannelloLogging.add(bottoneLogin);
		pannelloLogging.add(bottoneLogout);
		pannelloLogging.add(bottoneRegistrazione);
		
		
		//Inizializzazione e Inserimento Label
		titolo = new JLabel("Home");
		titolo.setForeground(Color.WHITE);
		pannelloTitolo.add(titolo);
		
		
		//END
		frame.pack();
		//frame.setSize(500,400);
		frame.setVisible(true);
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		
		
		
		//***** SEZIONE EVENTI *****//
		
		bottoneRegistrazione.addMouseListener(new MouseListener() {
			public void mouseReleased(MouseEvent e) {}
			public void mousePressed(MouseEvent e) {
				frame.setVisible(false);
				RegistrationWidget regWidget = new RegistrationWidget(lcmw);
			}
			public void mouseExited(MouseEvent e) {}
			public void mouseEntered(MouseEvent e) {}
			public void mouseClicked(MouseEvent e) {}
		});
		
		bottoneLogin.addMouseListener(new MouseListener() {
			public void mouseReleased(MouseEvent e) {}
			public void mousePressed(MouseEvent e) {
				frame.setVisible(false);
				LoginWidget logWidget = new LoginWidget(lcmw);
			}
			public void mouseExited(MouseEvent e) {}
			public void mouseEntered(MouseEvent e) {}
			public void mouseClicked(MouseEvent e) {}
		});
		
		bottoneLogout.addMouseListener(new MouseListener() {
			public void mouseReleased(MouseEvent e) {}
			public void mousePressed(MouseEvent e) {
				serviceGate.logout();
				JOptionPane.showMessageDialog(null, "Logout effettuato con successo", "Logout effettuato", JOptionPane.INFORMATION_MESSAGE);
			}
			public void mouseExited(MouseEvent e) {}
			public void mouseEntered(MouseEvent e) {}
			public void mouseClicked(MouseEvent e) {}
		});
		
		bottoneDisponibilita.addMouseListener(new MouseListener() {
			public void mouseReleased(MouseEvent e) {}
			public void mousePressed(MouseEvent e) {
				frame.setVisible(false);
				DisponibilitaWidget dispWidget = new DisponibilitaWidget(lcmw);
			}
			public void mouseExited(MouseEvent e) {}
			public void mouseEntered(MouseEvent e) {}
			public void mouseClicked(MouseEvent e) {}
		});
		
		bottonePartecipazioni.addMouseListener(new MouseListener() {
			public void mouseReleased(MouseEvent e) {}
			public void mousePressed(MouseEvent e) {
				frame.setVisible(false);
				PartecipazioneWidget partWidget = new PartecipazioneWidget(lcmw);
			}
			public void mouseExited(MouseEvent e) {}
			public void mouseEntered(MouseEvent e) {}
			public void mouseClicked(MouseEvent e) {}
		});
		
		bottoneNegoziazioni.addMouseListener(new MouseListener() {
			public void mouseReleased(MouseEvent e) {}
			public void mousePressed(MouseEvent e) {
				frame.setVisible(false);
				NegoziazioneWidget negWidget = new NegoziazioneWidget(lcmw);
			}
			public void mouseExited(MouseEvent e) {}
			public void mouseEntered(MouseEvent e) {}
			public void mouseClicked(MouseEvent e) {}
		});
			
	}
	
	void setVisible(boolean b) {
		if (b) {
			frame.setVisible(true);
		} else {
			frame.setVisible(false);
		}
	}

}